package com.cheung.leetcode.hash;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.hash
 * @Author :  cheungming
 * @CreateTime : 2024-05-02 11:02:58
 * @Description : 49题
 * @Version : 1.0
 */
public class Code49Test {


    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length <= 1) {
            return List.of(List.of(strs));
        }
        // 这个题目感觉不太行啊
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String next = new String(charArray);
            List<String> list = map.getOrDefault(next, new ArrayList<>());
            list.add(str);
            map.put(next, list);
        }

        return map.values().stream().toList();
    }


    @Test
    public void test1() {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(groupAnagrams(strs));
    }

    @Test
    public void test2() {
        String[] strs = {""};
        List<List<String>> result = new ArrayList<>();
        result.add(List.of(""));

        assert result.equals(groupAnagrams(strs));
    }


    @Test
    public void test3() {
        String[] strs = {"a"};
        List<List<String>> result = new ArrayList<>();
        result.add(List.of("a"));

        assert result.equals(groupAnagrams(strs));
    }

}
